package net.alegen.android.pizzashop.ui.order;

import net.alegen.android.pizzashop.R;
import net.alegen.android.pizzashop.order.Order;
import net.alegen.android.pizzashop.order.ToppingEnum;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ToppingLayoutFragment extends Fragment implements OnCheckedChangeListener {

	private CheckBox chkBoxPepperoni;
	private CheckBox chkBoxMushrooms;
	private CheckBox chkBoxVeggies;
	private CheckBox chkBoxTuna;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.topping_layout, null);
		
		this.chkBoxPepperoni = (CheckBox)v.findViewById(R.id.chkBoxPepperoni);
		this.chkBoxMushrooms = (CheckBox)v.findViewById(R.id.chkBoxMushrooms);
		this.chkBoxVeggies = (CheckBox)v.findViewById(R.id.chkBoxVeggies);
		this.chkBoxTuna = (CheckBox)v.findViewById(R.id.chkBoxTuna);
		
		this.chkBoxPepperoni.setChecked(true);
		this.chkBoxVeggies.setChecked(true);
		
		this.chkBoxPepperoni.setOnCheckedChangeListener(this);
		this.chkBoxMushrooms.setOnCheckedChangeListener(this);
		this.chkBoxVeggies.setOnCheckedChangeListener(this);
		this.chkBoxTuna.setOnCheckedChangeListener(this);
		
        return v;
    }

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked) {
			if(buttonView == this.chkBoxPepperoni)
				Order.addTopping(ToppingEnum.PEPPERONI);
			else if(buttonView == this.chkBoxMushrooms)
				Order.addTopping(ToppingEnum.MUSHROOMS);
			else if(buttonView == this.chkBoxVeggies)
				Order.addTopping(ToppingEnum.VEGGIES);
			else if(buttonView == this.chkBoxTuna)
				Order.addTopping(ToppingEnum.TUNA);
		} else {
			if(buttonView == this.chkBoxPepperoni)
				Order.removeTopping(ToppingEnum.PEPPERONI);
			else if(buttonView == this.chkBoxMushrooms)
				Order.removeTopping(ToppingEnum.MUSHROOMS);
			else if(buttonView == this.chkBoxVeggies)
				Order.removeTopping(ToppingEnum.VEGGIES);
			else if(buttonView == this.chkBoxTuna)
				Order.removeTopping(ToppingEnum.TUNA);
		}
	}
}
